#ifndef _USERMGNT_API_H_
#define _USERMGNT_API_H_

#include "global_common.h"
#include "mymd5.h"
#include "cJSON.h"
#include "xeSqlite.h"
#if 1
/**
 * @Date: 2020-05-27 10:32:10
 * @Description: tinybear add
 * @Return: 
 */
#define  NVR_MAX_GUICLIENT_NUM (128)
#define  NVR_MAX_NETCARD_NUM (2)

#define DB_FILE_2_MEMORY (0)
#define DB_MEMORY_2_FILE (1)
#endif


#define  USERMGNT_USERS_TBL     "users" 
#define  USERMGNT_GROUPS_TBL    "groups"

typedef struct{
    char channels[HISOME_LEN_256];///MAX support 512 channels 
    int preview; 
    int replay;
    int userMgnt;
    int PTZ;
    int storeMgnt;
    int sysCfg;
    int eventCfg;
    int networkCfg;
    int defaultUpgrade;
    int fileBackup;
    int ipcCfg;
    int sysInfo;
    int otherCfg;
}HisomeAccess;

typedef struct {
    char groupName[HISOME_LEN_128];
    HisomeAccess groupAccess;
    char remarks[HISOME_LEN_512];
}HisomeGroup;


typedef struct {
    int  userID;
    char userName[HISOME_LEN_128];
    char userTextPWD[HISOME_LEN_128];
    char user9CellPWD[HISOME_LEN_128];
    char protectAnswer1[HISOME_LEN_256];
    char protectAnswer2[HISOME_LEN_256];
    char protectAnswer3[HISOME_LEN_256]; 
    bool actived;
    HisomeGroup group;
    HisomeAccess userAccess;
    char remarks[HISOME_LEN_512];
    int  errLoginNum;
    int  lockTime;
    time_t  lastOperateTime;
    char loginIP[HISOME_LEN_128];
    time_t  loginTime;
}HisomeUser;


#define USERLOGIN_LOCKTIME (1800)
#define EXISTED (1)
#define NOT_EXIST (0)
#define MAX_ONLINE_USERNUM (128)

typedef struct _GUIClient{
    char gui_ip[HISOME_LEN_128];
}GUIClient;

extern HisomeUser g_loginUsers[MAX_ONLINE_USERNUM];
extern GUIClient g_GUIClientList[NVR_MAX_GUICLIENT_NUM];

extern bool   g_dbsToBakFile;
extern int g_needCRTE_UserMgntDB;


#if 1
#define  LOG_LOG_TBL  "oplog"



#define EN (0)
#define CN (1)



typedef enum _operateLogType{
  LOG_DISK_TYPE=0,
  LOG_SYSOPERATE_TYPE,
  LOG_CFGOPERATE_TYPE,
  LOG_ALARM_TYPE,
  LOG_USRMGNT_TYPE,
  LOG_FILEOPERATE_TYPE,
  LOG_LINK_TYPE,
  LOG_ALL_TYPE
}OperateLogType;

typedef struct _operateLog{
    char groupName[HISOME_LEN_128];
    char userName[HISOME_LEN_128];
    OperateLogType logType;
    char loginIP[HISOME_LEN_128];
    unsigned long recordTimeInt;
    char recordTimeStr[HISOME_LEN_128];
    char logText[HISOME_LEN_1024];
}OperateLog;

// inline void  apiOperateLog_WriteLog(char * groupName,char * userName,char * loginIP,char * logText,OperateLogType logType){
//     ;
// }



// inline int log_fillDB(sqlite3 *logDb){
//     ;
// }


#endif


int         userMgnt_fillDB(sqlite3 * userMgntDB);
int         userMgnt_DbmemToFile(sqlite3 * userMgntDB,char *fileName,int line);
HisomeUser* userMgnt_findOnlineUser(int userID);
void        userMgnt_addDftUser();


void        apiUsrMgnt_resetHisomeAccess(HisomeAccess *accsee,int enable);
void        apiUsrMgnt_resetHisomeGroup(HisomeGroup *group,int enable);
void        apiUsrMgnt_resetHisomeUser(HisomeUser *user,int enable);
int         apiUsrMgnt_groupIsExist(sqlite3 *db,HisomeGroup *group);
int         apiUsrMgnt_groupAdd(sqlite3 *db,HisomeGroup *group);
int         apiUsrMgnt_groupDelete(sqlite3 *db,HisomeGroup *group);
int         apiUsrMgnt_userIsExist(sqlite3 *db,HisomeUser *user);
int         apiUsrMgnt_userAdd(sqlite3 *db,HisomeUser *loginUser,HisomeUser *user);
int         apiUsrMgnt_userDelete(sqlite3 *db,HisomeUser *loginUser,HisomeUser *user);
HisomeUser *apiUsrMgnt_findOn_LINEUser(int userID);
void        apiUsrMgnt_deleteOn_LINEUser(int userID);
int         usrMgnt_addOnineUser(HisomeUser *user);
void        apiUsrMgnt_POST_Login(void *data);
void        apiUsrMgnt_DELETE_Logout(void *data);
void apiUsrMgnt_userid_keepalive(void *data);
void        apiUsrMgnt_QUERY_userMsg(void *data);
void        apiUsrMgnt_POST_groupAdd(void *data);
void        apiUsrMgnt_DELETE_groupDelete(void *data);
void        apiUsrMgnt_POST_groupModify(void *data);
void        apiUsrMgnt_POST_userAdd(void *data);
void        apiUsrMgnt_DELETE_userDelete(void *data);
void        apiUsrMgnt_POST_userModify(void *data);
void        apiUsrMgnt_POST_passwordReset(void *data);
void        apiUsrMgnt_DELETE_passwordProtect(void *data);
void        apiUsrMgnt_POST_IAmGUI(void *data);
#endif